package org.apache.http.entity.mime;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.apache.http.HttpEntity;
import org.apache.http.a.a;
import org.apache.http.entity.mime.content.ContentBody;

public class j {
    private static final char[] a = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private String b = "form-data";
    private e c = e.STRICT;
    private String d = null;
    private Charset e = null;
    private List f = null;

    j() {
    }

    private String a(String str, Charset charset) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("multipart/form-data; boundary=");
        stringBuilder.append(str);
        if (charset != null) {
            stringBuilder.append("; charset=");
            stringBuilder.append(charset.name());
        }
        return stringBuilder.toString();
    }

    public static j a() {
        return new j();
    }

    private String d() {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        int nextInt = random.nextInt(11) + 30;
        for (int i = 0; i < nextInt; i++) {
            stringBuilder.append(a[random.nextInt(a.length)]);
        }
        return stringBuilder.toString();
    }

    public j a(String str, ContentBody contentBody) {
        a.a((Object) str, "Name");
        a.a((Object) contentBody, "Content body");
        return a(new b(str, contentBody));
    }

    public j a(Charset charset) {
        this.e = charset;
        return this;
    }

    j a(b bVar) {
        if (bVar != null) {
            if (this.f == null) {
                this.f = new ArrayList();
            }
            this.f.add(bVar);
        }
        return this;
    }

    public j a(e eVar) {
        this.c = eVar;
        return this;
    }

    k b() {
        a dVar;
        String str = this.b != null ? this.b : "form-data";
        Charset charset = this.e;
        String d = this.d != null ? this.d : d();
        List arrayList = this.f != null ? new ArrayList(this.f) : Collections.emptyList();
        switch (this.c != null ? this.c : e.STRICT) {
            case BROWSER_COMPATIBLE:
                dVar = new d(str, charset, d, arrayList);
                break;
            case RFC6532:
                dVar = new f(str, charset, d, arrayList);
                break;
            default:
                dVar = new g(str, charset, d, arrayList);
                break;
        }
        return new k(dVar, a(d, charset), dVar.c());
    }

    public HttpEntity c() {
        return b();
    }
}
